shanbay = (function() {

  this.generate_id = function(id) {
    return "SHANBAY_ENHANCER_" + id;
  };

  this.await = function(fn_condition, fn_call, interval) {
    if (!interval)
      interval = 500;
    window.setTimeout(function() {
      if (fn_condition() === true) {
        fn_call();
      } else {
        window.setTimeout(arguments.callee, interval);
      }
    }, interval);
  };

  this.post = function(url, data, callback, type) {
    console.log("SE: post to: " + url);
    
    chrome.extension.sendRequest({
      action : "ajax",
      param : {
        type : "POST",
        url : url,
        data : data,
        dataType : type
      }
    }, function(response) {
      console.log("reply response: " + JSON.stringify(response));
      if (callback && response.result == "success") {
        callback(response.result, response.data);
      }
    });
  };

  this.user_info = {};
  $.get("http://www.shanbay.com/team/team/", function(data) {
    var $link = $(data).find(".nav-box:first a:first");
    if ($link.text().indexOf("回到我的小组") != -1) {
      $.extend(shanbay.user_info, {
        in_team : true,
        team_href : $link.prop("href"),
        team_img : $link.find("img:first").prop("src"),
      });
    } else {
      $.extend(shanbay.user_info, {
        in_team : false,
      });
    }
  });
  
  $.get("http://www.shanbay.com/api/user/info/", null, function(data) {
    if (data.result == 1) {
      $.extend(shanbay.user_info, {
        id : data.userid,
        name : data.username,
        nickname : data.nickname
      });
    }
  }, "json");
  

  //修复导航栏行为，点击该行的任意位置后跳转
  this.fix_nav_list = function(selector) {
    $(selector).click(function() {
      var a = $(this).children("a");
      window.location.href = a.prop("href");
    });
  };

  this.enhance = function() {
    //右侧导航标签增强
    shanbay.fix_nav_list(".nav-box li");

    //新版本检查
    $.getJSON(chrome.extension.getURL("manifest.json"), function(manifest) {
      var cur_version = manifest.version;

      var compare_version = function(ver1, ver2) {
        ver1 = ver1.split(".");
        ver2 = ver2.split(".");

        for ( var i in ver1) {
          if (parseInt(i) == ver2.length)
            return 1;
          var tmp = parseInt(ver1[i]) - parseInt(ver2[i]);
          if (tmp != 0)
            return tmp;
        }

        if (ver2.length > ver1.length)
          return -1;
        return 0;
      };

      var new_ver_href = "http://www.shanbay.com/forum/thread/85376/";
      $.get(new_ver_href, function(data) {
        try {
          var latest_version = $(data).find("#post-881278:first").text().match(
              /\[current_version=([0-9\.]+)\]/)[1];
          if (compare_version(cur_version, latest_version) >= 0)
            return;
        } catch (e) {
          console.log(e);
          return;
        }

        var ntf_html = "<li>扇贝助手有新版本可用。<a href='" + new_ver_href + "'>查看</a></li>";
        shanbay.ntf.append_ntf($(ntf_html));
      });

    });

  };

  return this;
})();

$(function() {
  shanbay.enhance();
});
